// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-18 14:02
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

public class MoveZeroes {
    /**
     * 移动0,把零元素移动数组后面
     */
    public void moveZeroes(int[] nums) {
        if (nums.length == 1) {
            return;
        }
        int p = 0, q = 0;
        while (nums[p] != 0) {
            p++;
            if (p >= nums.length) {
                return;
            }
        }
        while (nums[q] == 0 && q < p) {
            q++;
            if (q >= nums.length) {
                return;
            }
        }

        while (p < q && q < nums.length) {
            if (nums[q] != 0) {
                swap(nums, p, q);
                p++;
            }
            q++;
        }
    }

    public void swap(int[] nums, int p, int q) {
        int tmp = nums[p];
        nums[p] = nums[q];
        nums[q] = tmp;
    }

}
